package com.qf.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
   没有使用数据库连接池
 *
 */
public class JdbcUtil6 {

    private static final Properties PROPERTIES = new Properties();//存储配置文件的map


    //线程自带容器  就像线程的书包   把连接对象放在书包中
    private static final ThreadLocal<Connection> tl = new ThreadLocal<>();

    static {
        InputStream inputStream = JdbcUtil6.class.getClassLoader().getResourceAsStream("db.properties");
        //InputStream inputStream = JdbcUtil2.class.getResourceAsStream("/db.properties");
        //InputStream inputStream = JdbcUtil2.class.getResourceAsStream("db.properties");
        try {
            // 把配置文件中的数据 封装到 properties
            PROPERTIES.load(inputStream);

            Class.forName(PROPERTIES.getProperty("driverName"));

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    // 获取连接

    public static Connection getCon(){
        //先看书包里面有没有 有的话 就拿出来 ，要是没有 取出来的就是 null
        Connection connection = tl.get();

        if(connection==null){
            //书包里面没有  获取一个线的连接 然后放进书包
            try {
                //通过驱动管理器  来获取连接
                connection = DriverManager.getConnection(PROPERTIES.getProperty("url"), PROPERTIES.getProperty("userName"), PROPERTIES.getProperty("password"));
                // 往书包里面放东西
                tl.set(connection);

                return connection;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return connection;
    }

    // 关闭资源
    public static void closeAll(){
        //Connection connection = tl.get();

        Connection connection = getCon();
        System.out.println(connection);
        try {
            if(connection!=null){

                connection.close();

                tl.remove();  // 从书包移除
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 封装查询操作
     * @param sql  查询的sql 语句
     * @param args  可变长参数  接受 注入sql语句的 参数
     *
     * select * from t_user where username = ? and password = ? ;
     *
     * ["zs","123456"]
     *
     * executeQuery(sql, "zs","123456")
     * */

    public static ResultSet executeQuery(String sql, Object ...args){

        try {
            //获取连接
            Connection con = getCon();
            //Connection con = tl.get();
            System.out.println(con);
            PreparedStatement pstmt = con.prepareStatement(sql);
            if(args.length>0){
                for (int i = 0; i < args.length; i++) {
                    pstmt.setObject((i+1),args[i]);
                }
            }
            ResultSet resultSet = pstmt.executeQuery();
           // JdbcUtil3.closeAll(con);
            return resultSet;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    /**
     * 封装增删改操作
     *
     * */
    public static int executeUpdate(String sql, Object ...args){
        try {
            Connection con = getCon();
            PreparedStatement pstmt = con.prepareStatement(sql);
            if(args.length>0){
                for (int i = 0; i < args.length; i++) {
                    pstmt.setObject((i+1),args[i]);
                }
            }
            int result = pstmt.executeUpdate();
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }



    //开启事务
    public static void begin(){
        Connection connection = getCon();
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //提交事务
    public static void commit(){
        Connection connection = getCon();
        try {
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtil6.closeAll();
        }
    }
    //回滚事务
    public static void rollback(){
        Connection connection = getCon();
        try {
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtil6.closeAll();
        }
    }


    public static void main(String[] args) {

        Connection con = getCon();
        System.out.println(con);

        closeAll();

    }
}
